package com.lingjtx.common.other.helper;


import com.lingjtx.common.core.constant.Const;
import com.lingjtx.common.core.handle.ObjectPool;
import com.lingjtx.common.core.util.StringUtil;
import jakarta.mail.Message;
import jakarta.mail.Part;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class MailHelper {

    private static final Logger log = LoggerFactory.getLogger(MailHelper.class);

    public static Builder init() {
        return new Builder();
    }

    private static void send(Builder builder) {
        builder.objectPoolTemplate.consume(session -> {
            MimeMessage message = new MimeMessage(session);
            try {
                message.setFrom(new InternetAddress(builder.sender, Const.SERVER_NAME));
                message.setSubject(builder.subject);
                message.setDescription(Part.ATTACHMENT);
                message.setContent(builder.content, "text/html; charset=utf-8");
                Transport transport = session.getTransport();
                if (!transport.isConnected()) {
                    transport.connect();
                }
                String address = String.join(",", builder.receivers);
                message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address));
                transport.sendMessage(message, message.getAllRecipients());
                log.info("邮件发送成功: {}", address);
            } catch (Exception e) {
                log.error("邮件发送失败： {}", e.getMessage());
            }
        });
    }

    public static class Builder {

        private ObjectPool<Session> objectPoolTemplate;

        private String password;

        private String sender;

        private String subject;

        private String content;

        private List<String> receivers;

        public Builder withSubject(String subject) {
            this.subject = subject;
            return this;
        }

        public Builder withContent(String content) {
            this.content = content;
            return this;
        }

        public Builder withReceivers(List<String> receivers) {
            if (receivers == null || receivers.isEmpty()) {
                throw new NullPointerException("mail config receivers == null");
            }
            this.receivers = receivers;
            return this;
        }

        public Builder withPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder withSender(String sender) {
            this.sender = sender;
            return this;
        }

        public Builder withObjectPoolTemplate(ObjectPool<Session> objectPoolTemplate) {
            this.objectPoolTemplate = objectPoolTemplate;
            return this;
        }

        public void send() {
            if (objectPoolTemplate == null) {
                throw new NullPointerException("mail config objectPoolTemplate is null");
            }
            if (StringUtil.isEmpty(password)
                    || StringUtil.isEmpty(sender)) {
                password = Const.EMAIL_PASSWORD;
                sender = Const.EMAIL_SENDER;
            }
            MailHelper.send(this);
        }
    }
}